﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class eduExamPaper : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
             setdata();
    }

    //生成机考试题，生成两份试题
    protected void btnpaper_Click(object sender, EventArgs e)
    {
        //页面验证？？？？\
        //判断抽题数目总是小于总题数 
        bool bfind = false;
        int m_totalNum = 0;
        for (int t = 0; t < this.UltraWebGrid1.Rows.Count; t++)
        {
            if ((int)this.UltraWebGrid1.DisplayLayout.Rows[t].Cells[2].Value < (int)this.UltraWebGrid1.DisplayLayout.Rows[t].Cells[3].Value)
            {
                bfind = true;
                break;
            }
            else
                m_totalNum += (int)this.UltraWebGrid1.DisplayLayout.Rows[t].Cells[3].Value;
        }

        if (bfind == true)
            this.Label12.Text = "抽题数出错！";
        else
        {
            if (m_totalNum >= (int)this.WebNumericEdit2.Value)
            {
                CreatePaper(false, 2);
            }
            else
                this.Label12.Text = "通过考试需答对题目最少数不能大于试卷总题数！";
        } 
    }

    //生成现场考题
    protected void btnprintertest_Click(object sender, EventArgs e)
    {
        bool bfind = false;
        int m_totalNum = 0;
        for (int t = 0; t < this.UltraWebGrid1.Rows.Count; t++)
        {
            if ((int)this.UltraWebGrid1.DisplayLayout.Rows[t].Cells[2].Value < (int)this.UltraWebGrid1.DisplayLayout.Rows[t].Cells[3].Value)
            {
                bfind = true;
                break;
            }
            else
                m_totalNum += (int)this.UltraWebGrid1.DisplayLayout.Rows[t].Cells[3].Value;
        }

        if (bfind == true)
            this.Label12.Text = "抽题数大于题库中题目的总数！";
        else
        {
            if (m_totalNum >= (int)this.WebNumericEdit2.Value)
            {
                CreatePaper(true, 1);
            }
            else
                this.Label12.Text = "通过考试需答对题目最少数不能大于试卷总题数！";
        } 

        //显示纸质试卷，？？是弹出页面，还是其他的呢？？？
        Response.Redirect("~/eduExamPaperPrint.aspx?CourseID=" + Convert.ToInt32(Request.QueryString["CourseID"]) + "");
    }

    //更新页面的表格
    protected void UltraWebGrid1_UpdateGrid(object sender, Infragistics.WebUI.UltraWebGrid.UpdateEventArgs e)
    {
    }
    
    public void setdata()
    {
        SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["smsConnectionString"].ConnectionString);

        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("select CourseDocumentInfo.CourDocumentID,CourDocumentName from CourseInfo,CourseDocumentInfo,DocumentsInCourse where CourseInfo.CourseID=DocumentsInCourse.CourseID and CourseInfo.IsDeleted<>1 and DocumentsInCourse.CourDocumentID = CourseDocumentInfo.CourDocumentID and CourseDocumentInfo.IsDeleted <>1 and CourseInfo.CourseID='" + Convert.ToInt32(Request.QueryString["CourseID"]) + "' ", objConnection);
        da.Fill(ds, "Document");
        DataTable dt1 = ds.Tables["Document"];

        SqlDataAdapter da2 = new SqlDataAdapter("select * from QuestionInfo where IsDeleted<>1", objConnection);
        da2.Fill(ds, "QuestionInfo");
        DataTable dt2 = ds.Tables["QuestionInfo"];

        DataTable dt = new DataTable();
        DataColumn colWork = new DataColumn("CourDocumentID", System.Type.GetType("System.Int32"));
        dt.Columns.Add(colWork);
        colWork = new DataColumn("课程资料", System.Type.GetType("System.String"));
        dt.Columns.Add(colWork);
        colWork = new DataColumn("题库总题数", System.Type.GetType("System.Int32"));
        dt.Columns.Add(colWork);
        colWork = new DataColumn("抽题数目", System.Type.GetType("System.Int32"));
        dt.Columns.Add(colWork);

        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            DataRow row = dt.NewRow();
            row["CourDocumentID"] = (int)dt1.Rows[i].ItemArray[0];
            row["课程资料"] = dt1.Rows[i].ItemArray[1].ToString();

            DataRow[] rs = dt2.Select("CourDocumentID = '" + (int)dt1.Rows[i].ItemArray[0] + "'");
            row["题库总题数"] = rs.Length;
            row["抽题数目"] = 0;

            dt.Rows.Add(row);
            this.UltraWebGrid1.DataSource = dt;

            this.UltraWebGrid1.DataBind();
            this.UltraWebGrid1.Columns[0].Hidden = true;
            this.UltraWebGrid1.Columns[1].Width = 320;
            this.UltraWebGrid1.Columns[2].Width = 100;
            this.UltraWebGrid1.Columns[3].Width = 100;
        }

    }
    public void CreatePaper(bool mIsLocal,int mPaperNmu)
    {
        int m_nquestionnum ;
        SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["smsConnectionString"].ConnectionString);
        DataSet ds = new DataSet();
        SqlDataAdapter da;

        SqlDataAdapter da1 = new SqlDataAdapter("select * from ExamPaperInfo", objConnection);
        SqlCommandBuilder cb = new SqlCommandBuilder(da1);
        da1.Fill(ds, "ExamPaperInfo");
        DataTable dt1 = ds.Tables["ExamPaperInfo"];

        SqlDataAdapter da2 = new SqlDataAdapter("select * from CourseInfo", objConnection);
        SqlCommandBuilder cb2 = new SqlCommandBuilder(da2);
        da2.Fill(ds, "CourseInfo");
        DataTable dt2 = ds.Tables["CourseInfo"];

        DataRow[] rows = dt2.Select("CourseID = " + Convert.ToInt32(Request.QueryString["CourseID"]) + "");
        foreach (DataRow row in rows)
        {
            row["ExamAllTime"] = 60;
            row["PassQuestionNum"] = this.WebNumericEdit2.Value;
        }
        
        SqlCommand command = new SqlCommand();
        command.Connection = objConnection;
        command.CommandText = "Exam_deletePaperDetailID";
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Clear();

        SqlParameter paramCourseID = new SqlParameter("@CourseID", SqlDbType.Int);
        paramCourseID.Value = Convert.ToInt32(Request.QueryString["CourseID"]);
        command.Parameters.Add(paramCourseID);

       
        SqlParameter paramIsLocal = new SqlParameter("@IsLocal", SqlDbType.Bit);
        paramIsLocal.Value = mIsLocal;
        command.Parameters.Add(paramIsLocal);

        objConnection.Open();
        command.ExecuteNonQuery();
        objConnection.Close();

        for (int m_nPaperNum = 1; m_nPaperNum <= mPaperNmu; m_nPaperNum++)
        {
            m_nquestionnum = 1;
            command.CommandText = "Exam_findPaperDetailID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Clear();

            SqlParameter paramCourseID1 = new SqlParameter("@CourseID", SqlDbType.Int);
            paramCourseID1.Value = Convert.ToInt32(Request.QueryString["CourseID"]);
            command.Parameters.Add(paramCourseID1);

            SqlParameter paramExamPaperID = new SqlParameter("@ExamPaperID", SqlDbType.Int);
            paramExamPaperID.Value = m_nPaperNum;
            command.Parameters.Add(paramExamPaperID);

            SqlParameter paramIsLocal1 = new SqlParameter("@IsLocal", SqlDbType.Bit);
            paramIsLocal1.Value = mIsLocal;
            command.Parameters.Add(paramIsLocal1);

            SqlParameter paramIsDeleted = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            paramIsDeleted.Value = false;
            command.Parameters.Add(paramIsDeleted);

            SqlParameter OutputParaPaperDetailID = new SqlParameter("@PaperDetailID", SqlDbType.Int);
            command.Parameters.Add(OutputParaPaperDetailID);
            OutputParaPaperDetailID.Direction = ParameterDirection.Output;

            objConnection.Open();
            command.ExecuteNonQuery();
            int m_nPaperDetailID = (int)OutputParaPaperDetailID.Value;
            objConnection.Close();

            for (int i = 0; i < this.UltraWebGrid1.Rows.Count; i++)
            {
                da = new SqlDataAdapter("select top " + (int)this.UltraWebGrid1.DisplayLayout.Rows[i].Cells[3].Value + " * from QuestionInfo where IsDeleted<>1 and CourDocumentID = " + (int)this.UltraWebGrid1.DisplayLayout.Rows[i].Cells[0].Value + " order by newid()", objConnection);
                da.Fill(ds, "ExamQuestion");
                DataTable dt = ds.Tables["ExamQuestion"];

                for (int k = 0; k < dt.Rows.Count; k++)
                {
                    DataRow row = dt1.NewRow();
                    row["PaperDetailID"] = m_nPaperDetailID;
                    row["QuestionID"] = (int)dt.Rows[k].ItemArray[0];
                    row["QuestionNum"] = m_nquestionnum;
                    m_nquestionnum += 1;
                    dt1.Rows.Add(row);
                }
                dt.Clear();
            } 
        } 
        da2.Update(dt2);
        da1.Update(dt1);
        Response.Redirect("~/eduExamPaperEnd.aspx");

        //这个通过
        //DataRow[] foundRows = dt.Select("CourDocumentID = '1'", "CourDocumentID desc");
        //这个为什么不能通过阿？？？？？？？？？？郁闷！！！！！！！！！！！！
        //DataRow[] foundRows = dt.Select("CourDocumentID = '1'","newid()");
    }
}
